﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class User_ShoppingCart : System.Web.UI.Page
{
    ShoppingCartBUS cartBUS = new ShoppingCartBUS();
    ArrayList cartList;
    MessBUS mesBUS = new MessBUS();
    DataTable tbMess;
    BillBUS billBUS = new BillBUS();
    BillDetailsBUS billDetailsBUS = new BillDetailsBUS();
    BillGiftBUS billGiftBUS = new BillGiftBUS();
    validCardBUS validCard = new validCardBUS();
    Card card = new Card();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            cartList = (ArrayList)Session["GioHang"];
            cartBUS.TotalAmount();
            lblTotalAmount.Text = Session["TotalAmount"].ToString();
            dlShoppingCart.DataSource = cartList;
            dlShoppingCart.DataBind();
            LoadData();
            MultiView1.ActiveViewIndex = 0;
            RadioButtonList1.SelectedIndex = 0;
        }
    }
    private void LoadData()
    {
        tbMess = new DataTable();
        tbMess = mesBUS.returnAllMess();
        drlMessages.DataSource = tbMess;
        drlMessages.DataTextField = "Mes_Details";
        drlMessages.DataValueField = "Mes_ID";
        drlMessages.DataBind();
    }
    protected void dlShoppingCart_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        cartBUS.Del(Id);
        Response.Redirect("ShoppingCart.aspx");
    }
    protected void lbnUpdate_Click(object sender, EventArgs e)
    {
        int Quantity = 1;
        string idProduct = "";

        for (int i = 0; i < dlShoppingCart.Items.Count; i++)
        {
            Quantity = Int32.Parse(((TextBox)dlShoppingCart.Items[i].FindControl("txtNumber")).Text);
            idProduct = dlShoppingCart.DataKeys[i].ToString();
            cartBUS.Update(idProduct, Quantity);
        }
        Response.Redirect("ShoppingCart.aspx?id=" + Quantity + " " + idProduct);
    }
    protected void lbnContinue_Click(object sender, EventArgs e)
    {
        Response.Redirect("Home.aspx");
    }
    protected void drlMessages_TextChanged(object sender, EventArgs e)
    {
        txtMessages.Text = drlMessages.SelectedItem.ToString();
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedIndex == 0)
        {
            DatePicker1.Enabled = false;
        }
        else
        {
            DatePicker1.Enabled = true;
        }
    }
    protected void lbnNext_Click(object sender, EventArgs e)
    {

        if (txtAddress.Text.Equals(""))
        {
            lblErrorAddress.Text = "You must enter address";
        }
        else
        {
            lblErrorAddress.Text = "";
            if (RadioButtonList1.SelectedValue == "2" && DatePicker1.SelectedDate.ToShortDateString().Equals("1/1/0001"))
            {
                lblDate.Text = "You must choose date";
            }
            else
            {
                if (RadioButtonList1.SelectedValue == "2" && DatePicker1.SelectedDate <= DateTime.Now)
                {
                    lblDate.Text = "The date delivered have to be after now";
                }
                else
                {
                    if (Session["ID"] == null)
                    {
                        Page.Controls.Add(new LiteralControl("<script language='javascript'> window.alert(\"" + "You must login" + "\"); <" + "/script>"));
                    }
                    else
                    {
                        lblDate.Text = "";
                        MultiView1.ActiveViewIndex = 1;
                        LoadView2();
                    }

                }

            }
        }
    }
    private void LoadView2()
    {
        for (int i = 1; i <= 12; i++)
        {
            drlMonth.Items.Add(new ListItem(i.ToString()));
        }
        for (int j = 1900; j < 2013; j++)
        {
            drlYear.Items.Add(new ListItem(j.ToString()));
        }
    }
    protected void lbnComplete_Click(object sender, EventArgs e)
    {
        float totalAmount = 0;
        string address = "";
        string date = "";
        string datecom = "";
        string memID = "";
        string mess = "";
        string status = "No delivered";
        DataTable tbBill = new DataTable();

        string bouquetID = "";
        int bidQuantity = 0;
        float bidAmount = 0;
        int biID = 0;

        bool check1 = false;
        bool check2 = false;
        // MultiView1.ActiveViewIndex = 2;
        if (txtName.Text.Equals("") || txtNumberCard.Text.Equals("") || txtCVN.Text.Equals(""))
        {
            lblError2.Text = "You must enter enough information";
        }
        else
        {
            
            card = validCard.CheckCC(txtNumberCard.Text);
            if (card.CardType.Equals("VISA") || card.CardType.Equals("MasterCard"))
            {
                totalAmount = float.Parse(Session["TotalAmount"].ToString());
                address = txtAddress.Text;
                date = DateTime.Now.ToString();
                memID = Session["ID"].ToString();
                if (txtMessages.Text.Equals(""))
                {
                    mess = "";
                }
                else
                {
                    mess = txtMessages.Text;
                }
                if (RadioButtonList1.SelectedIndex == 0)
                {
                    DateTime temp = DateTime.Now;
                    temp = temp.AddHours(5);
                    datecom = temp.ToString();
                }


                bool check = billBUS.insertBill(totalAmount, address, date, datecom, memID, mess, status);
                if (check)
                {
                    tbBill = billBUS.returnBillByManyFiels(totalAmount, date, datecom, address, memID);
                    DataRow row1 = tbBill.Rows[0];
                    biID = Int32.Parse(row1["Bi_ID"].ToString());
                    ArrayList shopping = (ArrayList)Session["GioHang"];
                    foreach (ShoppingCart cart in shopping)
                    {
                        bouquetID = cart.Id;
                        bidQuantity = cart.Number;
                        bidAmount = cart.Amount;

                        if (cutString(bouquetID).Equals("HH"))
                        {
                            check1 = billDetailsBUS.insertBillDetails(biID, bouquetID, bidQuantity, bidAmount);
                        }
                        else
                        {
                            check2 = billGiftBUS.insertBillGift(biID, bouquetID, bidQuantity, bidAmount);

                        }
                    }
                    if (check1 == true || check2 == true)
                    {
                        MultiView1.ActiveViewIndex = 2;
                    }
                    else
                    {
                        Response.Write("Not Insert");
                    }
                }
            }
            else
            {
                lblError2.Text = "Number card not valid";
            }

        }


    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session["GioHang"] = null;
        Session["SLItem"] = null;
        Session["TotalAmount"] = null;
        Session["Gift"] = null;
        Response.Redirect("Home.aspx");
    }
    protected void lbnDestroy_Click(object sender, EventArgs e)
    {
        Session["GioHang"] = null;
        Session["SLItem"] = null;
        Session["TotalAmount"] = null;
        Session["Gift"] = null;
        Response.Redirect("Home.aspx");
    }
    private string cutString(string arr)
    {
        char[] buf = arr.ToCharArray();
        string result = "";
        for (int i = 0; i < 2; i++)
        {
            result += buf[i];
        }
        return result;
    }
}